package com.songcha.sakura.mvvm.home.schedule

import com.google.gson.JsonParser
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.exception.NoDataException
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.bean.ComicScheduleListBean
import com.songcha.sakura.config.BaseConfig
import com.songcha.library_database_sakura.greendao.GreenDaoManager
import com.songcha.library_database_sakura.greendao.HomeScheduleCacheBean

import io.reactivex.Observable
import org.jsoup.Jsoup

class HomeScheduleRepository : BaseRepository()  {

    fun updateScheduleList(bean:ComicScheduleListBean){
        val homeScheduleCacheDao= GreenDaoManager.getInstance().homeScheduleCacheDao
        val list=homeScheduleCacheDao.loadAll()
        val cacheBean= HomeScheduleCacheBean()
        cacheBean.weeks=bean.toString()

        if(list.size==0){
            homeScheduleCacheDao.insert(cacheBean)
        }else{
            cacheBean.id=list[0].id
            homeScheduleCacheDao.update(cacheBean)
        }

        SharedPreferenceHelper.setInt("home_cache","schedule_cache_time",(System.currentTimeMillis()/1000).toInt())
    }

    fun getScheduleListByDB():Observable<ComicScheduleListBean> {
        return createObservable {
            val homeScheduleCacheDao= GreenDaoManager.getInstance().homeScheduleCacheDao
            val list=homeScheduleCacheDao.loadAll()

            if(list.size>0){
                val scheduleList=mutableListOf<MutableList<ComicListBean.ComicBean>>()

                val str=list[0].weeks
                val jsonParser= JsonParser()
                val json=jsonParser.parse(str).asJsonObject
                val data=json.get("data").asJsonArray

                for(i in 0 until data.size()){
                    val item=data[i]
                    val temp=mutableListOf<ComicListBean.ComicBean>()
                    val arr=item.asJsonObject.get("week").asJsonArray

                    for(j in 0 until arr.size()){
                        val item2=arr[j].asJsonObject
                        val title=item2.get("title").asString
                        val href=item2.get("href").asString.replace("\\/","/")
                        val update=item2.get("update").asString

                        val bean=ComicListBean.ComicBean(title,href,"",update,"","","")
                        temp.add(bean)
                    }

                    scheduleList.add(temp)
                }

                val scheduleListBean=ComicScheduleListBean(scheduleList)
                it.onNext(scheduleListBean)
                it.onComplete()
            }else{
                it.onError(NoDataException())
            }
        }
    }

    fun getScheduleList(): Observable<ComicScheduleListBean> {
          return createNetworkObservable {
              val data=mutableListOf<MutableList<ComicListBean.ComicBean>>()
              val document=Jsoup.connect("${BaseConfig.BASE_URL_WINDMILL}").get()
              val elements=document.getElementsByClass("tlist")[0].getElementsByTag("ul")

              if(elements.size==7){
                  for(i in 0 until 7){
                      val temp=mutableListOf<ComicListBean.ComicBean>()
                      val e=elements[i]
                      val lis=e.getElementsByTag("li")
                      for(li in lis){
                          val a=li.child(1)
                          val href=a.attr("href")
                          val title=a.attr("title")
                          val update=li.getElementsByTag("span")[0].text()

                          val b=ComicListBean.ComicBean(title,href,"",update,"","","")
                          temp.add(b)
                      }
                      data.add(temp)
                  }
              }

              val bean=ComicScheduleListBean(data)
              it.onNext(bean)
              it.onComplete()

          }

    }

}